package cn.itcast.nems.studentrecord.web.controller.b;

import cn.hutool.core.lang.Assert;
import cn.itcast.ic.common.bean.Page;
import cn.itcast.ic.common.exception.v2.BizExceptionProducer;
import cn.itcast.ic.common.exception.v2.bean.BizStatusEnum;
import cn.itcast.nems.common.PageQueryModel;
import cn.itcast.nems.common.web.vo.BasePageRequest;
import cn.itcast.nems.studentrecord.dto.ReceivableAdjustmentDTO;
import cn.itcast.nems.studentrecord.dto.ReceivableAdjustmentQueryDTO;
import cn.itcast.nems.studentrecord.enumeration.QueryRangeTypeEnum;
import cn.itcast.nems.studentrecord.service.ReceivableAdjustmentService;
import cn.itcast.nems.studentrecord.web.util.BusinessApprovalFormWebUtil;
import cn.itcast.nems.studentrecord.web.vo.form.ReceivableAdjustmentEditVO;
import cn.itcast.nems.studentrecord.web.vo.query.ReceivableAdjustmentQueryVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author ChangLu.Gao
 * @version v1.0
 * @program nems-server
 * @description
 * @date 2023/11/22 17:48
 */
@Slf4j
@RestController
@RequestMapping("/b/receivable/adjustment")
@RequiredArgsConstructor
public class ReceivableAdjustmentController {

    private final ReceivableAdjustmentService receivableAdjustmentService;

    /**
     * 调价单分页查询
     *
     * @param pageRequest 请求条件
     * @return org.springframework.http.ResponseEntity<cn.itcast.ic.common.bean.Page < cn.itcast.nems.studentrecord.dto.ReceivableAdjustmentDTO>>
     * @author 高昌露
     * @since 2023/11/22 18:02
     */
    @PostMapping("/{rangeType}/page/list")
    public ResponseEntity<Page<ReceivableAdjustmentDTO>> page(@PathVariable String rangeType, @RequestBody BasePageRequest<ReceivableAdjustmentQueryVO> pageRequest) {
        QueryRangeTypeEnum rangeTypeEnum = QueryRangeTypeEnum.of(rangeType.toUpperCase());
        Assert.notNull(pageRequest.getCondition(), BizExceptionProducer.badRequest("查询条件不可为空"));
        Assert.notNull(pageRequest.getCondition().getRangeType(), BizExceptionProducer.badRequest(BizStatusEnum.BAD_REQUEST.toString(), "查询条件:%s不可为空", "审批范围"));
        Assert.notNull(pageRequest.getCondition().getMinCreatedDatetime(), BizExceptionProducer.badRequest(BizStatusEnum.BAD_REQUEST.toString(), "查询条件:%s不可为空", "时间范围"));
        Assert.notNull(pageRequest.getCondition().getMaxCreatedDatetime(), BizExceptionProducer.badRequest(BizStatusEnum.BAD_REQUEST.toString(), "查询条件:%s不可为空", "时间范围"));
        pageRequest.convert();
        pageRequest.getCondition().setRangeType(rangeTypeEnum.name());
        PageQueryModel<ReceivableAdjustmentQueryDTO> queryModel = new PageQueryModel<>(
                pageRequest.getPageNumber(),
                pageRequest.getPageSize(),
                BusinessApprovalFormWebUtil.convert(pageRequest.getCondition())
        );
        return ResponseEntity.ok(receivableAdjustmentService.pageList(queryModel));
    }

    /**
     * 编辑调价单数据并存储为草稿
     *
     * @param request 请求参数
     * @return org.springframework.http.ResponseEntity<cn.itcast.nems.studentrecord.dto.ReceivableAdjustmentDTO>
     * @author 高昌露
     * @since 2023/11/23 16:49
     */
    @PostMapping("/edit")
    public ResponseEntity<ReceivableAdjustmentDTO> saveOrEdit(@RequestBody ReceivableAdjustmentEditVO request) {
        Assert.notNull(request, BizExceptionProducer.badRequest("请求数据不可为空"));
        Assert.notNull(request.getStudentRecordId(), BizExceptionProducer.badRequest("学籍ID不可为空"));
        Assert.notNull(request.getReason(), BizExceptionProducer.badRequest("调价原因不可为空"));
        Assert.notNull(request.getAmount(), BizExceptionProducer.badRequest("调整金额不可为空"));
        String id = receivableAdjustmentService.saveOrEdit(BusinessApprovalFormWebUtil.convert(request));
        return ResponseEntity.ok(receivableAdjustmentService.queryById(id));
    }

    /**
     * 根据学籍ID获取调价单详情
     * @author 高昌露
     * @since 2023/11/23 16:55
     * @param studentRecordId 学籍ID
     * @return org.springframework.http.ResponseEntity<cn.itcast.nems.studentrecord.dto.ReceivableAdjustmentDTO>
     */
    @GetMapping("/student/{studentRecordId}")
    public ResponseEntity<ReceivableAdjustmentDTO> queryByStudentId(@PathVariable String studentRecordId) {
        return ResponseEntity.ok(receivableAdjustmentService.queryByStudentRecord(studentRecordId));
    }

    /**
     * 根据学籍ID获取调价单详情
     *
     * @param id 调价单
     * @return org.springframework.http.ResponseEntity<cn.itcast.nems.studentrecord.dto.ReceivableAdjustmentDTO>
     * @author 高昌露
     * @since 2023/11/23 16:55
     */
    @GetMapping("/{id}")
    public ResponseEntity<ReceivableAdjustmentDTO> queryById(@PathVariable String id) {
        return ResponseEntity.ok(receivableAdjustmentService.queryById(id));
    }


    /**
     * 根据学籍ID逻辑删除数据
     *
     * @param id 调价单
     * @return org.springframework.http.ResponseEntity<cn.itcast.nems.studentrecord.dto.ReceivableAdjustmentDTO>
     * @author 高昌露
     * @since 2023/11/23 16:55
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<String> delete(@PathVariable String id) {
        receivableAdjustmentService.delete(id);
        return ResponseEntity.ok(id);
    }


    /**
     * 提交调价单
     *
     * @param request 请求参数
     * @return org.springframework.http.ResponseEntity<cn.itcast.nems.studentrecord.dto.ReceivableAdjustmentDTO>
     * @author 高昌露
     * @since 2023/11/23 16:49
     */
    @PostMapping("/commit")
    public ResponseEntity<ReceivableAdjustmentDTO> commitToBPM(@RequestBody ReceivableAdjustmentEditVO request) {
        Assert.notNull(request, BizExceptionProducer.badRequest("请求数据不可为空"));
        Assert.notNull(request.getStudentRecordId(), BizExceptionProducer.badRequest("学籍ID不可为空"));
        Assert.notNull(request.getReason(), BizExceptionProducer.badRequest("调价原因不可为空"));
        Assert.notNull(request.getAmount(), BizExceptionProducer.badRequest("调整金额不可为空"));
        String journalId = this.receivableAdjustmentService.commitToBPM(BusinessApprovalFormWebUtil.convert(request));
        ReceivableAdjustmentDTO result = this.receivableAdjustmentService.queryById(journalId);
        return ResponseEntity.ok(result);
    }
}
